package com.bosssoft.promotions.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.base.enums.ResultCodeEnum;
import com.bosssoft.promotions.domain.dto.PromotionQueryDTO;
import com.bosssoft.promotions.domain.dto.UserPromotionDTO;
import com.bosssoft.promotions.service.PromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/promotion")
@Api(value = "促销活动方法类")
public class PromotionController {

    @Autowired
    private PromotionService promotionService ;



    @ApiOperation(value = "查询商品可用的促销活动")
    @PostMapping("queryQualifiedPromotions")
    public Result queryQualifiedPromotion(@RequestBody PromotionQueryDTO promotionQueryDTO){
        List<String> promotions = promotionService.justice(promotionQueryDTO.getPromotionId(),promotionQueryDTO.getIds());
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "查询商品可用促销活动成功", promotions);
    }



    @ApiOperation(value = "使用相应的优惠")
    @PostMapping("usePromotion")
    public Result usePromotion(@RequestBody UserPromotionDTO userPromotionDTO){

        BigDecimal finalPrice = new BigDecimal(0);
        Map<String,BigDecimal> map = new HashMap<>();
        if("打折活动".equals(userPromotionDTO.getType())) {
             finalPrice = promotionService.getDiscount(userPromotionDTO.getPromotionId(), userPromotionDTO.getPrice());
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "打折成功", finalPrice);
        }else if("套装活动".equals(userPromotionDTO.getType())){
             finalPrice = promotionService.getSuit(userPromotionDTO.getPromotionId(), userPromotionDTO.getIds());
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "套装成功", finalPrice);
        }else if("定金活动".equals(userPromotionDTO.getType())){
            map = promotionService.getDeposit(userPromotionDTO.getPromotionId(), userPromotionDTO.getPrice());
        }else{
            finalPrice = promotionService.getFullReduction(userPromotionDTO.getPromotionId(), userPromotionDTO.getPrice());
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "满减成功", finalPrice);
        }

        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "定金成功", map);
    }
}
